package com.terrier.ui.core
{
    import flash.geom.Rectangle;

    /**
     * 组件容器的内部容器
     *
     * @author Jarvis.weng
     */
    public class WxContentPane extends WxComponent
    {
        private var _clipping:Boolean = false;

        /**
         * 构造函数
         */
        public function WxContentPane()
        {
            super();
			
			_concernChildLayout = true;
        }

        /**
         * 所有子对象的最大边界。
         * @return
         */
        public function get childrenBound():Rectangle
        {
            var maxBound:Rectangle = new Rectangle();
            for (var i:int = 0; i < numChildren; ++i)
            {
                var childBound:Rectangle = getChildAt(i).getBounds(this);
                maxBound.left = Math.min(maxBound.left, childBound.left);
                maxBound.top = Math.min(maxBound.top, childBound.top);
                maxBound.right = Math.max(maxBound.right, childBound.right);
                maxBound.bottom = Math.max(maxBound.bottom, childBound.bottom);
            }
            return maxBound;
        }

        /**
         * 是否剪裁显示对象
         * @return
         */
        public function get clipping():Boolean
        {
            return _clipping;
        }

        /**
         * @private
         */
        public function set clipping(value:Boolean):void
        {
            if (_clipping == value)
            {
                return;
            }

            if (value)
            {
                super.scrollRect = new Rectangle(0, 0, width, height);
            }
            else
            {
                super.scrollRect = null;
            }

            _clipping = value;
        }

        /**
         * 设置宽度，高度
         * @param w 宽度
         * @param h 高度
         */
        public function setSize(w:Number, h:Number):void
        {
            if (clipping)
            {
                var rect:Rectangle = super.scrollRect;
                rect.width = w;
                rect.height = h;
                super.scrollRect = rect;
            }
            else
            {
                width = w;
                height = h;
            }
        }

        /**
         * 显示区域x轴滚动距离
         * @return
         */
        public function get scrollX():Number
        {
            return clipping ? super.scrollRect.x : 0;
        }

        /**
         * @private
         */
        public function set scrollX(value:Number):void
        {
            if (clipping)
            {
                var rect:Rectangle = super.scrollRect;
                rect.x = value;
                super.scrollRect = rect;
            }
        }

        /**
         * 显示区域y轴滚动距离
         * @return
         */
        public function get scrollY():Number
        {
            return clipping ? super.scrollRect.y : 0;
        }

        /**
         * @private
         */
        public function set scrollY(value:Number):void
        {
            if (clipping)
            {
                var rect:Rectangle = super.scrollRect;
                rect.y = value;
                super.scrollRect = rect;
            }
        }

        /**
         * 剪裁区域的宽度
         * @return
         */
        override public function get width():Number
        {
            return clipping ? super.scrollRect.width : super.width;
        }

        /**
         * @private
         */
        override public function set width(value:Number):void
        {
            if (clipping)
            {
                var rect:Rectangle = super.scrollRect;
                rect.width = value;
                super.scrollRect = rect;
            }
            else
            {
                super.width = value;
            }
        }

        /**
         * @private
         */
        override public function get height():Number
        {
            return clipping ? super.scrollRect.height : super.height;
        }

        /**
         * @private
         */
        override public function set height(value:Number):void
        {
            if (clipping)
            {
                var rect:Rectangle = super.scrollRect;
                rect.height = value;
                super.scrollRect = rect;
            }
            else
            {
                super.height = value;
            }
        }

        /**
         * @private
         */
        override public function set scrollRect(value:Rectangle):void
        {
            // 屏蔽原有功能
        }
    }
}
